<?php
namespace Admin\Model;
use \Frame\Libs\BaseModel;

final class BookModel extends BaseModel{
	public function fetchBookByPages($startrows,$pagesize){
		$sql = "SELECT * FROM love_book LIMIT $startrows,$pagesize";
		return $this->pdo->fetchAll($sql);
	}
	
	public function fetchAll(){
	    $sql = "SELECT * FROM love_book";
		return $this->pdo->fetchAll($sql);
	}
	
	public function rowCount(){
		$sql = "SELECT book_id FROM love_book";
		return $this->pdo->rowCount($sql);
	}
	
	//在love_book表计数isbn
	public function isExistBook($isbn){
		$sql = "SELECT book_id FROM love_book WHERE book_isbn='$isbn'";
		return $this->pdo->rowCount($sql);
	}
	
	//插入一条新纪录
	public function addNewBook($name,$isbn,$inventory){
		$sql = "INSERT INTO love_book (book_isbn,book_name,book_inventory) VALUES ('$isbn','$name','$inventory')";
		return $this->pdo->exec($sql);
	}
	
	//获取book_id
	public function getBookId($isbn){
		$sql = "SELECT book_id FROM love_book WHERE book_isbn='$isbn'";
		return $this->pdo->fetchOne($sql);
	}
	
	//增加库存
	public function updateBookInventory($book_id,$inventory){
		$sql = "UPDATE love_book SET book_inventory=book_inventory+".$inventory." WHERE `book_id`='$book_id'";
		return $this->pdo->exec($sql);
	}
	
	//获取一条
	public function fetchOne($id){
		$sql = "SELECT * FROM love_book WHERE book_id='$id'";
		return $this->pdo->fetchOne($sql);
	}
	
	//更新
	public function editBook($id,$name,$isbn,$inventory,$out){
		$sql = "UPDATE love_book SET book_isbn='$isbn',book_name='$name',book_out='$out',book_inventory='$inventory' WHERE book_id='$id'";
		return $this->pdo->exec($sql);
	}
	
	//删除
	public function delBook($id){
		$sql = "DELETE FROM love_book WHERE book_id='$id'";
		return $this->pdo->exec($sql);
	}
}
?>